﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Perovich.GameObjects.Attributes;
using Microsoft.Xna.Framework;

namespace Perovich.GameObjects.Components
{
    [AutomaticProperty("Width", "Size2D.Width", XmlSummary = "Gets or sets the width of the entity.", XmlReturns = "An <see cref=\"Int32\">Int32</see> that is the entity's width")]
    [AutomaticProperty("Height", "Size2D.Height", XmlSummary = "Gets or sets the height of the entity.", XmlReturns = "An <see cref=\"Int32\">Int32</see> that is the entity's height")]
    [AutomaticProperty("Size", "Size2D.Size", XmlSummary = "Gets or sets the size of the entity.", XmlReturns = "A <see cref=\"Size\">Size</see> that is the entity's size")]
    [AutomaticProperty("Origin", "Size2D.Origin", XmlSummary = "Gets or sets the origin of the entity.", XmlReturns = "A <see cref=\"Vector2\">Vector2</see> that is the entities origin.")]
    [AutomaticImport("System.Drawing", false)]
    [AutomaticImport("System.Drawing", true)]
    public sealed class Size2D : EntityComponent
    {
        Size sz;
        public Size2D()
        {
            sz = new Size(0, 0);
            Origin = Vector2.Zero;
        }

        public int Width
        {
            get
            {
                return sz.Width;
            }
            set
            {
                sz = new Size(value, sz.Height);
            }
        }

        public int Height
        {
            get
            {
                return sz.Height;
            }
            set
            {
                sz = new Size(sz.Width, value);
            }
        }

        public Size Size
        {
            get
            {
                return sz;
            }
            set
            {
                sz = value;
            }
        }

        public Vector2 Origin { get; set; }


        public ISizeable SynchWith { get; set; }

        private Transform2D trans;

        public override void Initialize()
        {
            UpdateOrder = Constants.SizeUpdateOrder;
            trans = Owner.Find<Transform2D>();
            Owner.ComponentAdded += new EventHandler<EntityComponentEventArgs>(Owner_ComponentAdded);
            Owner.ComponentRemoved += new EventHandler<EntityComponentRemovedEventArgs>(Owner_ComponentRemoved);
            base.Initialize();
        }

        void Owner_ComponentRemoved(object sender, EntityComponentRemovedEventArgs e)
        {
            if (e.Component is Transform2D)
                trans = null;
        }

        void Owner_ComponentAdded(object sender, EntityComponentEventArgs e)
        {
            Transform2D t;
            if ((t = e.Component as Transform2D) != null)
                trans = t;
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (SynchWith != null)
            {
                if (trans == null)
                {
                    Size = SynchWith.Size;
                }
                else
                {
                    Size = new Size((int)(SynchWith.Size.Width * trans.Scale), (int)(SynchWith.Size.Height * trans.Scale));
                }
                Origin = SynchWith.Origin;
            }
            base.Update(gameTime);
        }
    }
}
